<?php

namespace App\Admin\Controllers;

use App\Models\User;

use App\Models\UserLevel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('会员');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('会员');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('会员');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column("name", "昵称");
            $grid->column("phone", '手机');
            $grid->column("money", '收益');
            $grid->levelDetail()->level_name("会员等级");
            $grid->status("状态")->display(function ($status) {
                if ($status == 0) {
                    return "<span class='label-default label'>未审核</span>";
                } elseif ($status == 1) {
                    return "<span class='label-primary label'>正常</span>";
                } else {
                    return "<span class='label-warning label'>驳回</span>";
                }
            });
            $grid->created_at("注册时间");
            $grid->updated_at("更新时间");
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

           $levels = UserLevel::all();
            $form->display('id', 'ID');
            $form->text("name", "昵称")->rules('required', [
                'required' => '昵称为必填字段',
            ]);
            $form->text("phone", '手机')->rules('required');
            $form->password("password", '密码')->rules('required|confirmed')
                ->default(function ($form) {
                    return $form->model()->password;
                });;
            $form->password('password_confirmation', "密码确认")->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });
            $options = [];
            foreach ($levels as $level){
                $options[$level->id] = $level->level_name;
            }
            $form->select('level', '会员等级')->options($options);
            $form->ignore(['password_confirmation']);

            $form->switch("status", '状态');
            $form->display('created_at', '注册时间');
            $form->display('updated_at', '更新时间');

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });

        });
    }
}
